// tester.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
void print_usage (void);

int _tmain(int argc, _TCHAR* argv[])
{	//STEP 1 - parse parameters
	char lab_name[10];
	char test_name[10];
	int test_num;

	if(argc<2) {print_usage(); exit(0);}
	char path_to_test[200];
	char result_filename[200];
	strcpy(result_filename,"result.txt");
	char report[10];
	if(argc==4) 
		strcpy(report,"0");
	else 
		strcpy(report,argv[4]);

	sprintf(lab_name,"Lab_%s",argv[2]);
	sscanf(argv[3], "%d", &test_num);
	sprintf(test_name,"%02d",test_num);



	sprintf(path_to_test,".\\%s\\%s\\%s",lab_name,test_name,report);
	char command_line[300];
	char main_test[120];
	sprintf(main_test,"%s\\tests.txt",path_to_test);
	//STEP 2 - run auto tester
	char toenv[4][40];
	sprintf(toenv[0],"labnumber=%s",argv[2]);
	sprintf(toenv[1],"variantnumber=%s",argv[3]);
	sprintf(toenv[2],"reportnumber=%s",report);
	char *envp[4]={toenv[0],toenv[1],toenv[2],NULL};
	spawnle(_P_WAIT,"auto_tester.exe","auto_tester.exe",argv[1],path_to_test,test_name,NULL,envp);	
	//STEP 3 - create results file
	spawnl(_P_WAIT,"tester_merge.exe","tester_merge.exe",argv[1],lab_name,test_name,report,NULL);
	//STEP 4 - run WinDiff
	sprintf(command_line,"WinDiff.exe result.tst %s",result_filename);
	system(command_line);
	//STEP 5 - cleanup
	remove(result_filename);
	system("del *.tst");
	return 0;
}


void print_usage (void)
{	//0 - program name
	// 1 - exe file to test
	// 2 - lab catalog
	// 3 - task catalog
	// 4 - report catalog (0 if it home work)
	printf("Simple automatic tester by POAS, version 0.01\n");
	printf("Usage: auto_tester.exe <file to test with path> <lab name> <individual task name> [<report task name>]\n");
	printf("If report task absent assume 0 as report name\n\n");
	printf("Directory .\\<lab name>\\<individual task name>\\<report task name> must contain main test file tests.txt and test files\n");
	printf("Two strings in tests.txt define a test: first contains name of test, second - name of file with input\n");
	printf("The tester will create file result.txt in current directory with full test log\n");
	printf("WARNING: Tested program must read all input from stdin and doesn't use conio.h finctions like getch()\n");
}
